﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ChinaUnicom.Views
{
    public partial class StatisticsPage : PhoneApplicationPage
    {
        ApplicationBarIconButton[] statisticsButtons;

        void InitializeStatisticsButtons()
        {
            statisticsButtons = new ApplicationBarIconButton[3];

            statisticsButtons[0] = new ApplicationBarIconButton();
            statisticsButtons[0].IconUri = new Uri("/Themes/Images/6mos.checked.png", UriKind.Relative);
            statisticsButtons[0].Text = "最近半年";
            statisticsButtons[1] = new ApplicationBarIconButton();
            statisticsButtons[1].IconUri = new Uri("/Themes/Images/12mos.png", UriKind.Relative);
            statisticsButtons[1].Text = "最近一年";
            statisticsButtons[2] = new ApplicationBarIconButton();
            statisticsButtons[2].IconUri = new Uri("/Themes/Images/all.png", UriKind.Relative);
            statisticsButtons[2].Text = "全部";

            foreach (ApplicationBarIconButton button in statisticsButtons)
            {
                button.Click += OnStatisticsClick;
                ApplicationBar.Buttons.Add(button);
            }
        }


        public StatisticsPage()
        {
            InitializeComponent();
            InitializeStatisticsButtons();
        }

        private void OnStatisticsClick(object sender, EventArgs e)
        {
            ApplicationBarIconButton current = sender as ApplicationBarIconButton;

            if (current.IsChecked()) { return; }

            foreach (ApplicationBarIconButton button in statisticsButtons)
            {
                button.UpdateState(false);
            }

            current.UpdateState(true);
        }

        private void OnBackClick(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

    }

    static class ApplicationBarIconButtonEx
    {
        public static void UpdateState(this ApplicationBarIconButton button, bool @checked)
        {
            string url = button.IconUri.OriginalString;

            if (@checked && !url.Contains("checked"))
            {
                // checked
                string[] parts = url.Split(new[] { '.' });
                button.IconUri = new Uri(string.Format("{0}.checked.{1}", parts[0], parts[1]), UriKind.Relative);
            }
            else if (url.Contains("checked"))
            {
                // unchecked
                string[] parts = url.Split(new[] { '.' });
                button.IconUri = new Uri(string.Format("{0}.{1}", parts[0], parts[2]), UriKind.Relative);
            }
        }

        public static bool IsChecked(this ApplicationBarIconButton button)
        {
            return button.IconUri.OriginalString.Contains("checked");
        }
    }
}